﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAOMall;
using System.Data;
using System.Data.SqlClient;

namespace DTOMall
{
    public class BooksDTO : DBTool, ICommon
    {
        public bool insert(object obj)
        {
            if (obj == typeof(Books))
            {
                try
                {
                    using (SqlConnection con = GetConnection())
                    {
                        Books b = (Books)obj;
                        using (SqlCommand cmd = con.CreateCommand())
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "Insert_Books";
                            cmd.Parameters.AddWithValue("@movie_id", b.movie_id);
                            cmd.Parameters.AddWithValue("price", b.price);
                            cmd.Parameters.AddWithValue("@quantity", b.quantity);
                            cmd.Parameters.AddWithValue("@status", b.status);
                            cmd.Parameters.AddWithValue("@fullname", b.fullname);
                            cmd.Parameters.AddWithValue("@address", b.address);
                            cmd.Parameters.AddWithValue("@email", b.email);
                            cmd.Parameters.AddWithValue("@phone", b.phone);
                            cmd.Parameters.AddWithValue("@card_code", b.card_code);
                            cmd.Parameters.AddWithValue("@description", b.description);
                            cmd.Parameters.AddWithValue("@createdDate", b.created_date);
                            object o = cmd.ExecuteScalar();
                            CloseConnection();
                            if (o == typeof(int))
                            {
                                return ((int)o > 0) ? true : false;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }

            }
            else
            {
                return false;
            }
        }

        public bool update(object obj)
        {
            if (obj == typeof(Books))
            {
                try
                {
                    using (SqlConnection con = GetConnection())
                    {
                        Books b = (Books)obj;
                        using (SqlCommand cmd = con.CreateCommand())
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "Update_Books";
                            cmd.Parameters.AddWithValue("@id", b.id);
                            cmd.Parameters.AddWithValue("@movie_id", b.movie_id);
                            cmd.Parameters.AddWithValue("price", b.price);
                            cmd.Parameters.AddWithValue("@quantity", b.quantity);
                            cmd.Parameters.AddWithValue("@status", b.status);
                            cmd.Parameters.AddWithValue("@fullname", b.fullname);
                            cmd.Parameters.AddWithValue("@address", b.address);
                            cmd.Parameters.AddWithValue("@email", b.email);
                            cmd.Parameters.AddWithValue("@phone", b.phone);
                            cmd.Parameters.AddWithValue("@card_code", b.card_code);
                            cmd.Parameters.AddWithValue("@description", b.description);
                            cmd.Parameters.AddWithValue("@modifiedDate", b.created_date);
                            object o = cmd.ExecuteScalar();
                            CloseConnection();
                            if (o == typeof(int))
                            {
                                return ((int)o > 0) ? true : false;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            else
            {
                return false;
            }
        }

        public bool delete(int id)
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "Delete_Books";
                        cmd.Parameters.AddWithValue("@id", id);
                        object o = cmd.ExecuteScalar();
                        if (o == typeof(int))
                        {
                            return ((int)o > 0) ? true : false;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public System.Data.DataTable getAll()
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {

                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "View_All_Books";
                        DataTable dt = GetTable(cmd);
                        return dt;
                    }

                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public object getByID(int id)
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "View_Books_By_ID";
                        cmd.Parameters.AddWithValue("@id", id);
                        DataTable dt = GetTable(cmd);
                        Books b = new Books();
                        b.id = id;
                        b.movie_id = Convert.ToInt32(dt.Rows[0]["name"].ToString());
                        b.price = Convert.ToSingle(dt.Rows[0]["name"].ToString());
                        b.quantity = Convert.ToInt32(dt.Rows[0]["name"].ToString());
                        b.status = Convert.ToInt32(dt.Rows[0]["name"].ToString());
                        b.fullname = dt.Rows[0]["name"].ToString();
                        b.address = dt.Rows[0]["name"].ToString();
                        b.email = dt.Rows[0]["name"].ToString();
                        b.phone = dt.Rows[0]["name"].ToString();
                        b.card_code = dt.Rows[0]["name"].ToString();
                        b.description = dt.Rows[0]["name"].ToString();
                        return b;
                    }

                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
